
package pe.com.yugi.DAO.Impl;

import java.io.Serializable;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import pe.com.yugi.DAO.CombinacionDAO;
import pe.com.yugi.Dominio.Carta;
import pe.com.yugi.Dominio.Combinacion;
import pe.com.yugi.Util.HibernateUtil;


public class CombinacionDAOImpl implements CombinacionDAO, Serializable{

    @Override
    public boolean agregar(Combinacion c) {
        boolean agreg = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(c);
            session.getTransaction().commit();
            agreg = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return agreg;
    }

    @Override
    public List<Combinacion> listarCombinacion() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("from Combinacion");
        List<Combinacion> list = query.list();
        for(Combinacion c : list){
            Hibernate.initialize(c.getCarta());
        }
        return list;
    }

    @Override
    public Combinacion obtenerUltimaCombinacion() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query query = session.createQuery("SELECT c FROM Combinacion c WHERE idCombinacion = ( SELECT MAX( idCombinacion ) FROM Combinacion )");
        Combinacion list = (Combinacion) query.uniqueResult();
        Hibernate.initialize(list.getCarta());
        return list;
    }
    
}
